/*
 * @Author: chengsl
 * @Date: 2022-11-03 16:44:55
 * @LastEditors: chengsl
 * @LastEditTime: 2022-11-03 18:19:04
 * @Description: 封装公用的响应 JSON 结构
 */
function getTwoNum(num) {
  return (+num < 10 ? '0' : '')+ num
}
class JsonDto {
  constructor({code, data, message, status, timestamp} = {}) {
    this.code = code || -1 // 业务响应code
    this.data = data || null // 响应数据
    this.message = message || '' // 消息说明
    this.status = status || -1 // 业务响应status
    // this.methodType = '' // 请求类型
    this.author = '老程' // 作者
    this.timestamp = timestamp || this.getNowTime() // 请求时间戳
  }
  setCode(code) {
    this.code = code
    return this
  }
  setData(data) {
    this.data = data
    return this
  }
  setMessage(message) {
    this.message = message
    return this
  }
  setStatus(status) {
    this.status = status
    return this
  }
  setMethodType(value) {
    this.methodType = value
    return this
  }
  toString() {
    return JSON.stringify(this)
  }
  getNowTime() {
    const now = new Date();
    return `${now.getFullYear()}-${getTwoNum(now.getMonth() + 1)}-${getTwoNum(now.getDate())} ${getTwoNum(now.getHours())}:${getTwoNum(now.getMinutes())}:${getTwoNum(now.getSeconds())}`
  }
}

module.exports = {
  JsonDto
}